package com.my.screenadapter.data;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.ajie.lib_base.ft_file.FileIOUtils;
import com.my.screenadapter.R;
import com.my.screenadapter.shell.ShellExec;
import com.tencent.mmkv.MMKV;

import java.io.IOException;

/**
 * 测试 跨进程，多app 同时 读写，是否支持。
 */
public class MmkvActivity extends AppCompatActivity {

    private static final String TAG = "MmkvActivity";
    private MMKV mmkv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mmkv);

        MMKV.initialize("/bbox/media/");
        mmkv = MMKV.mmkvWithID("comm", MMKV.MULTI_PROCESS_MODE);
    }

    int count = 0 ;
    public void set(View view) {
        // TODO: 2020/9/30 false
        boolean encode = mmkv.encode("persist.bbox.timezoneid", count++);
        Log.i(TAG, "set: result:"+encode);
    }

    public void foreach(View view) {
        String[] strings = mmkv.allKeys();
        // TODO: 2020/9/30  null
        for (int i = 0; strings!=null && i < strings.length; i++) {
            Log.i(TAG, "foreach: key:"+strings[i]+"  v:"+mmkv.getString(strings[i],"")+"  v,decode:"+mmkv.decodeString(strings[i]));
        }

    }

    public void echoBeeboxesFile(View view) {
        //TODO java.io.IOException: Permission denied
        FileIOUtils.writeFileFromString("/bbox/media/abccc.txt","test");
        try {
            ShellExec.execute("echo abc > /bbox/media/abc.txt");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}